package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.AlipayMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.MchntAcctInfoMapper;
import cc.rengu.igas.bomp.common.dao.MchntBaseInfoMapper;
import cc.rengu.igas.bomp.common.dao.MchntImageInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.AlipayMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.MchntAcctInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.MchntBaseInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.MchntImageInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.AlipayMchntInfo;
import cc.rengu.igas.bomp.common.entity.MchntAcctInfo;
import cc.rengu.igas.bomp.common.entity.MchntBaseInfo;
import cc.rengu.igas.bomp.common.entity.MchntImageInfo;
import cc.rengu.igas.bomp.common.enums.*;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.BankCardInfoBean;
import cc.rengu.igas.bomp.facade.bean.ImageInfoBean;
import cc.rengu.igas.bomp.facade.request.AlipayActivityApplyViewRequest;
import cc.rengu.igas.bomp.facade.response.AlipayActivityApplyViewResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.SysParamMapper;
import cc.rengu.oltp.service.common.dao.impl.SysParamMapperImpl;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by zhangxuran on 2020/03/25
 */
public class AlipayActivityApplyViewService extends ManageService {
    private AlipayActivityApplyViewResponse alipayActivityApplyViewResponse = new AlipayActivityApplyViewResponse();

    @Override
    protected Object bizBeforeCust() throws Exception {
        AlipayActivityApplyViewRequest alipayActivityApplyViewRequest = new AlipayActivityApplyViewRequest();
        ConvertUtil.convertOutput(alipayActivityApplyViewRequest);
        return alipayActivityApplyViewRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        AlipayActivityApplyViewRequest alipayActivityApplyViewRequest = (AlipayActivityApplyViewRequest) request;

        /*获取法人行号信息*/
        String instId = alipayActivityApplyViewRequest.getHeader().getInstId();
        /* 获取用户标识，商户号信息 */
        String userId = alipayActivityApplyViewRequest.getUserId();
        /*获取商户号信息*/
        String mchntNo = alipayActivityApplyViewRequest.getMchntNo();
        /*必填性校验*/
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mchntNo)) {
            rglog.error("必填性校验失败!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /* 根据商户号查询商户基本信息 */
        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        MchntBaseInfo mchntBaseInfo = mchntBaseInfoMapper.selectMchntBaseInfoByPrimaryKey(instId, mchntNo);
        /*正常状态商户信息判断*/
        if (null == mchntBaseInfo || !MchntStatusEnum.NORMAL.getStatus().equals(mchntBaseInfo.getMchntStatus())) {
            rglog.error("商户基本信息为空!");
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        /* 根据商户号信息查询支付宝商户信息*/
        AlipayMchntInfoMapper alipayMchntInfoMapper = new AlipayMchntInfoMapperImpl();
        AlipayMchntInfo alipayMchntInfo = alipayMchntInfoMapper.selectAlipayMchntInfoByPrimaryKey(instId, mchntNo);
        if (null == alipayMchntInfo) {
            rglog.error("支付宝商户信息为空!");
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        /* 根据商户号查询银行卡信息 */
        BankCardInfoBean bankCardInfo = new BankCardInfoBean();
        MchntAcctInfoMapper mchntAcctInfoMapper = new MchntAcctInfoMapperImpl();
        MchntAcctInfo mchntAcctInfo = mchntAcctInfoMapper.selectMchntAcctInfoByPrimaryKey(instId, mchntNo, BaseProdEnum.ALL.getProdCode(), FundTypeEnum.SETTLE_TYPE.getFundCode());
        if (null == mchntAcctInfo) {
            rglog.error("商户结算账户信息为空!");
        } else {
            /*结算账户户名赋值*/
            bankCardInfo.setBankCardName(mchntAcctInfo.getSettleAcctName());
            /*结算账户号赋值*/
            bankCardInfo.setBankCardNo(mchntAcctInfo.getSettleAcctNo());
            /*开户行赋值*/
            bankCardInfo.setBankName(mchntAcctInfo.getAcctOpenBankName());
        }
        /*根据支付宝MCC码通过参数表查询必验图片类型
         **/
        SysParamMapper sysParamMapper = new SysParamMapperImpl();
        SysParam sysParam = sysParamMapper.selectSysParamByPrimaryKey(AppParamConstant.DEFAULT_INSTID, "alipayActivityImgType", alipayMchntInfo.getAlipayCategory());
        if (null != sysParam) {
            String imgTypeParam = sysParam.getParamValue();
            /*多个类型用逗号（,）拼接*/
            String[] imgTypes = imgTypeParam.split(",");
            MchntImageInfoMapper mchntImageInfoMapper = new MchntImageInfoMapperImpl();
            if (imgTypes.length > 0) {
                for (String imgType : imgTypes) {
                    List<MchntImageInfo> mchntImageInfoList = mchntImageInfoMapper.selectMchntImageInfoByMchntNoAndImageType(instId, mchntNo, imgType);
                    AlpayActivityImageEnum alpayActivityImageEnum = AlpayActivityImageEnum.getImageTypeEnumByImageType(imgType);
                    if (null == alpayActivityImageEnum) {
                        continue;
                    }
                    if (!CollectionUtils.isEmpty(mchntImageInfoList)) {
                        /*当同类型图片存在多张时默认获取第一张*/
                        MchntImageInfo mchntImageInfo = mchntImageInfoList.get(0);
                        ImageInfoBean imageInfoBean = new ImageInfoBean();
                        /*非业务主键赋值*/
                        imageInfoBean.setId(mchntImageInfo.getId());
                        /*支付宝图片标识赋值*/
                        imageInfoBean.setAlipayImageId(mchntImageInfo.getAlipayImageId());
                        /*微信图片标识赋值*/
                        imageInfoBean.setWechatImageId(mchntImageInfo.getWechatMediaId());
                        /*影响图片序号赋值*/
                        imageInfoBean.setImageIndex(String.valueOf(mchntImageInfo.getImageIndex()));
                        /*图片路径赋值*/
                        imageInfoBean.setImagePath(mchntImageInfo.getImageRemotePath());
                        /*图片描述赋值*/
                        imageInfoBean.setImageDesc(mchntImageInfo.getImageDesc());

                        resImage(alpayActivityImageEnum.getName(), imageInfoBean);
                    }
                }
            }
        }
        /*组装商户信息*/
        alipayActivityApplyViewResponse.setMchntNo(mchntBaseInfo.getMchntNo());
        alipayActivityApplyViewResponse.setMchntName(mchntBaseInfo.getMchntName());
        /*组装支付宝商户信息*/
        alipayActivityApplyViewResponse.setSpecialRateApplyFlag(alipayMchntInfo.getSpecialRateApplyFlag());
        alipayActivityApplyViewResponse.setActivityApplyId(alipayMchntInfo.getActivityApplyId());
        alipayActivityApplyViewResponse.setActivityType(alipayMchntInfo.getActivityType());
        alipayActivityApplyViewResponse.setActivityApplyResult(alipayMchntInfo.getActivityApplyResult());
        alipayActivityApplyViewResponse.setApplyRefuseResoan(alipayMchntInfo.getAuditRefuseReason());
        /* 组装结算账户银行卡信息 */
        alipayActivityApplyViewResponse.setBankCardInfo(bankCardInfo);
        /* 组装图片信息 */
        /* 按照接口转换支付宝特殊费率申请信息并响应给请求方 */
        BizResponse<AlipayActivityApplyViewResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(alipayActivityApplyViewResponse);
        return bizResponse;
    }

    /*根据属性名称进行赋值*/
    private void resImage(String fieldName, ImageInfoBean imageInfo) {
        try {
            Field field = alipayActivityApplyViewResponse.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(alipayActivityApplyViewResponse, imageInfo);
        } catch (Exception e) {
            rglog.error("设置图像信息错误！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        AlipayActivityApplyViewResponse alipayActivityApplyViewResponse = (AlipayActivityApplyViewResponse) bizResponse.getResult();
        ConvertUtil.convertInput(alipayActivityApplyViewResponse);
    }
}
